﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ASPNETCORETEST.Controllers
{
    [Route("Home")]
    public class HomeController : Controller
    {
        [Route("Index")]
        public IActionResult Index()
        {
            return View();
        }

        [Route("Say")]
        public IActionResult Say()
        {
            return Content("hello,I'm bingling.");
        }

        [HttpPost]
        [Route("Save")]
        public async Task<IActionResult> Save()
        {
            try
            {
                StreamReader sr = new(Request.Body);
                string base64 = await sr.ReadToEndAsync();
                sr.Close();

                MemoryStream stream = new (Convert.FromBase64String(base64.Split(',')[1]));
                Bitmap bitmap = new (stream);
                string filename = "Imgs/"+DateTime.Now.ToString("HHmmssfff") + ".png";
                bitmap.Save(filename, ImageFormat.Png);
                return Content("success");
            }
            catch (Exception exception)
            {
                return Content(exception.Message);
            }
        }
    }
}
